#include "form_prompt_dlg.h"
#include "ui_form_prompt_dlg.h"


/*
 * Function:    FormPromptWidget
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormPromptWidget::FormPromptWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormPromptWidget)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * FormPromptWidget:    ~FormPromptWidget
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormPromptWidget::~FormPromptWidget()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormPromptWidget" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormStartupTest" class
 */
FormPromptWidget* FormPromptWidget::getInstance(QWidget *pParent)
{
    static FormPromptWidget *pNew = NULL;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormPromptWidget(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_initialze();

        // Initialize control's UI
        pNew->_initUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _initialze
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormPromptWidget::_initialze()
{
    qDebug() << Q_FUNC_INFO;

    if(!m_pTimer) m_pTimer = new QTimer(this);
    Q_ASSERT(m_pTimer);

    if(!m_pLineLoading) m_pLineLoading = new QLineLoading(this);
    Q_ASSERT(m_pLineLoading);

    // Signal-slot initialization
    std::vector<bool> vecConnect;
    vecConnect.push_back(QObject::connect(m_pTimer, SIGNAL(timeout()), this, SLOT(slotHideWindow()), Qt::AutoConnection));
    for(int i=0; i<(int)vecConnect.size(); i++) {
        Q_ASSERT(vecConnect.at(i));
    }
}

/*
* Function:		_initUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormPromptWidget::_initUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/share/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->label_Prompt_Info->setWordWrap(true);

    // Adjust the position
    this->move(0, 0);
    this->hide();
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormPromptWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);

    switch (e->type())
    {
    case QEvent::LanguageChange:
        qDebug() << Q_FUNC_INFO;

        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

/*
* Function:		on_pushButton_Save_clicked
* Description:	Save the basic info after click the button
* Paramer:		void
* Return:		void
*/
void FormPromptWidget::on_pushButton_Confirm_clicked()
{
    qDebug() << Q_FUNC_INFO;

    m_pTimer->stop();
    m_pLineLoading->pause();

    this->hide();
}

/*
* Function:		slotHideWindow
* Description:	Timeout & hide window
* Paramer:		void
* Return:		void
*/
void FormPromptWidget::slotHideWindow()
{
    qDebug() << Q_FUNC_INFO;

    m_pTimer->stop();
    m_pLineLoading->pause();

    this->hide();
}

/*
* Function:		setPromptInfo
* Description:	Close the window after click the button
* Paramer:		QString sTitle - title name
*               QString sPromptInfo - prompt info
*               bool bShowConform - is show conform button
*               int nTimeout - timeout
* Return:		void
*/
void FormPromptWidget::setPromptInfo(QString sTitle, QString sPromptInfo, bool bShowConform , int nTimeout)
{
    qDebug() << Q_FUNC_INFO << sTitle << sPromptInfo << bShowConform << nTimeout;

    // Update the prompt info
    ui->label_PageTitle->setText(sTitle);
    ui->label_Prompt_Info->setText(sPromptInfo);
    ui->pushButton_Confirm->setVisible(bShowConform);

    if(0 < nTimeout) {
        m_pTimer->setInterval(nTimeout);
        m_pTimer->start();

        if(!bShowConform) {
            m_pLineLoading->start();
            m_pLineLoading->move(this->width() / 2 - m_pLineLoading->size().width() / 2, this->height() / 2 + 80);
        }
    }
    else {
        m_pLineLoading->pause();
        m_pTimer->stop();
    }

    this->setGeometry(0, 0, this->width(), this->height());
    this->show();
}

/*
 * Function:    HideWindow
 * Description: Hide the prompt window
 * Paramer:     void
 * Return:      void
 */
void FormPromptWidget::hideWindow()
{
    qDebug() << Q_FUNC_INFO;

    this->slotHideWindow();
}
